package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import com.sky.vo.CategorySelectIdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理")
public class CategoryController {
   @Resource
   CategoryService categoryService;


   @GetMapping("page")
   @ApiOperation("分类分页查询")
   public Result<PageResult> selectPageCategory (CategoryPageQueryDTO dto){
      Result<PageResult> pageResultResult = categoryService.selectPageCategory(dto);
      return pageResultResult;
   }
   @PutMapping()
   @ApiOperation("修改分类")
   public Result reviseCategory(@RequestBody CategoryDTO category){
      log.info("前端传过来的修改分类参数:{}",category);
     categoryService.reviseCategory(category);
      return Result.success("新增成功");
   }

   @PostMapping("status/{status}")
   @ApiOperation("启用、禁用分类")
   public Result reviseStatus(@PathVariable(name="status") Integer status,Long id){
      categoryService.reviseStatus(status,id);
      return Result.success("更新成功");
   }

   @PostMapping()
   @ApiOperation("新增分类")
   public Result insertCategory(@RequestBody CategoryDTO category){
      log.info("前端传过来的新增分类参数:{}",category);
      categoryService.insertCategory(category);
      return Result.success("新增成功");
   }

   @GetMapping("list")
   @ApiOperation("根据类型查询分类")
   public Result<List<CategorySelectIdVO>> selectIdCategory(@RequestParam Integer type){
      log.info("根据类型查询分类的参数{}",type);
     List<CategorySelectIdVO> categorySelectIdVOS = categoryService.selectTypeCategory(type);
      return Result.success(categorySelectIdVOS);
   }
   @DeleteMapping
   @ApiOperation("删除分类")
   public Result deletCategory(@RequestParam Integer id){
      int i = categoryService.deleteById(id);
      return i==1?Result.success("删除成功"):Result.error("删除失败");
   }

}
